var shop;
var shop1;
var shop2;
$(function () {
    window.sessionStorage.setItem("item",'{"shopid":0,"areaid":0}')
    render(JSON.parse(window.sessionStorage.getItem("item")).shopid,JSON.parse(window.sessionStorage.getItem("item")).areaid);
    choose($(".choose li").eq(0), "http://mmb.ittun.com/api/getgsshop", "choose", shop);
    choose($(".choose li").eq(1), "http://mmb.ittun.com/api/getgsshoparea", "choose1", shop1);
    choose1($(".choose li").eq(2),"choose2",shop2);
    $(".search a").click(function () {
        if ($(this).hasClass("fa-tasks")) {
            $(".inc").css({
                'opacity': 1,
                'visibility': 'visible'
            }).find("a").click(function () {
                console.log($(this).parent())
                console.log($(".a1"))
                if ($(this).parent()[0] == $(".a1")[0]) {
                    $(".inc .a1").find("a").removeClass("active");
                    $(this).addClass("active");
                } else {
                    $(".inc .a2").find("a").removeClass("active");
                    $(this).addClass("active");
                }
            })
            $(this).removeClass("fa fa-tasks").addClass("fa fa-close");
        } else {
            $(".inc").css({
                'opacity': 0,
                'visibility': 'hidden'
            });
            $(this).removeClass("fa fa-close").addClass("fa fa-tasks");
        }
    })
})

function choose(ele, url, mod, shop) {
    ele.click(function () {
        $(".none").css({
            'visibility': 'hidden'
        });
        ele.siblings("li").find(".fa").removeClass().addClass("fa fa-caret-down")
        if (ele.find(".fa").hasClass("fa-caret-down")) {
            ele.find(".fa").removeClass("fa-caret-down").addClass("fa-caret-up");
            $(".none").css({
                'visibility': 'visible'
            });
        } else {
            ele.find(".fa").removeClass("fa-caret-up").addClass("fa-caret-down");
            $(".none").css({
                'visibility': 'hidden'
            });
        }
        $.ajax({
            url: url,
            success: function (backData) {
                for (var i = 0; i < backData.result.length; i++) {
                    backData.result[i].shop = shop;
                }
                var result = template(mod, backData);
                $(".none").html(result);
                $(".none li").click(function () {
                    $(".none li").find(".fa").removeClass("fa fa-check");
                    $(this).find(".fa").addClass("fa fa-check");
                    shop = '' + $(this).index()
                    $(".none").css({
                        'visibility': 'hidden'
                    });
                    ele.find(".fa").removeClass("fa-caret-up").addClass("fa-caret-down");
                    if(ele[0]==$(".choose li").eq(0)[0]){
                        console.log();
                        window.sessionStorage.setItem("item",'{"shopid":'+parseInt($(this).attr("shopId"))+',"areaid":'+JSON.parse(window.sessionStorage.getItem("item")).areaid+'}');
                        ele.find(".prix").text($(this).text());
                    }else{
                        window.sessionStorage.setItem("item",'{"shopid":'+JSON.parse(window.sessionStorage.getItem("item")).shopid+',"areaid":'+parseInt($(this).attr("areaId"))+'}');
                        ele.find(".prix").text($(this).text().trim().split("（")[0]);
                    }
                    
                    render(JSON.parse(window.sessionStorage.getItem("item")).shopid,JSON.parse(window.sessionStorage.getItem("item")).areaid);
                })
            }
        }) 
    })
}
function choose1(ele, mod, shop) {
    ele.click(function () {
        $(".none").css({
            'visibility': 'hidden'
        });
        ele.siblings("li").find(".fa").removeClass().addClass("fa fa-caret-down")
        if (ele.find(".fa").hasClass("fa-caret-down")) {
            ele.find(".fa").removeClass("fa-caret-down").addClass("fa-caret-up");
            $(".none").css({
                'visibility': 'visible'
            });
        } else {
            ele.find(".fa").removeClass("fa-caret-up").addClass("fa-caret-down");
            $(".none").css({
                'visibility': 'hidden'
            });
        }
        
        var backData={'result':[{'price':'全部价格'},{'price':'1-3元'},{'price':'3-5元'},{'price':'5-10元'},{'price':'10-15元'},{'price':'15元以上'}]};
        for (var i = 0; i < backData.result.length; i++) {
            backData.result[i].shop = shop;
        }
        var result = template(mod, backData);
        console.log(result);
        $(".none").html(result);
        $(".none li").click(function () {
            $(".none li").find(".fa").removeClass("fa fa-check");
            $(this).find(".fa").addClass("fa fa-check");
            ele.find(".prix").text($(this).text());
            shop = '' + $(this).index()
            $(".none").css({
                'visibility': 'hidden'
            });
            ele.find(".fa").removeClass("fa-caret-up").addClass("fa-caret-down");
            render(parseInt($(this).attr("shopid")));
        })
        render(JSON.parse(window.sessionStorage.getItem("item")).shopid,JSON.parse(window.sessionStorage.getItem("item")).areaid);
    })
}
function render(shopid,areaId) {
    $.ajax({
        url: "http://mmb.ittun.com/api/getgsproduct",
        data: {
            shopid:shopid,
            areaid:areaId
        },
        success: function (backData) {
            var result = template("list", backData);
            $(".list").html(result);
        }
    })
}
